package com.jwork.progress.service;

import com.jwork.progress.enums.CalOperatorEnums;
import com.jwork.progress.params.CommonResponse;
import com.jwork.progress.pojo.dto.calculator.GetCalResDTO;
import com.jwork.progress.pojo.vo.calculator.CalResVO;
import com.jwork.progress.service.strategy.OperatorStrategy;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class CalculateService {
    private static Logger logger = LoggerFactory.getLogger(CalculateService.class);
    private static final Map<String, OperatorStrategy> operators = new HashMap();
    private final ConsumerService consumerService;

    public CommonResponse getRes(GetCalResDTO dto) {
        for (CalOperatorEnums value : CalOperatorEnums.values()) {
            if (value.getCode().equals(dto.getOpName())) {
                OperatorStrategy operatorStrategy = value.getOperatorStrategy();
                int res = operatorStrategy.execute(dto.getA(), dto.getB());
                CalResVO calResVO = new CalResVO();
                calResVO.setCalRes(res);
                logger.info("策略计算结果{}", calResVO);
                return CommonResponse.success(calResVO);
            }
        }
        consumerService.showRecords();
        return CommonResponse.fail("无运算表达式");
    }
}
